package org.zjx.app.action.login;


import org.zjx.analysis.analyze.login.LoginServerAnalysis;
import org.zjx.command.state.AccountState;
import org.zjx.window.action.Action;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;

public class LoginServerAction extends Action {
    protected LoginServerAnalysis loginServerAnalysis;

    public LoginServerAction(IWindow targetWindow) {
        super(targetWindow, false);
        loginServerAnalysis = new LoginServerAnalysis();
        loginServerAnalysis.analyze(targetWindow.getScreenshot());
    }

    @Override
    protected void doExecute() throws Exception {
        AccountState accountState = targetWindow.getState().getState(AccountState.class);

        String[] main = accountState.getMainLocation().split(",");
        String[] sub = accountState.getSubLocation().split(",");

        if (loginServerAnalysis.isAgreePage(targetWindow.getScreenshot())) {
            Rectangle agreeButtonRectangle = loginServerAnalysis.getAgreeButtonRectangle();
            targetWindow.actions().clickAt(agreeButtonRectangle.getCenterX(), agreeButtonRectangle.getCenterY());
        }else {
            targetWindow.actions().clickAt(Integer.parseInt(main[0]), Integer.parseInt(main[1]))
                    .delay(300)
                    .clickAt(Integer.parseInt(sub[0]), Integer.parseInt(sub[1]));
        }
        targetWindow.execute(WindowOperation.delay(1000));
    }
}
